<template>
  <div class="interview-experience">
    <div class="header">
      <div class="header-left">
        <h1>企业面试经验分享</h1>
        <p class="description">分享和查看真实的面试经验，提升求职成功率</p>
      </div>
      <el-button type="primary" size="large" @click="goToCreate">
        <el-icon><Plus /></el-icon>
        分享我的经验
      </el-button>
    </div>

    <!-- 搜索栏 -->
    <el-card shadow="never" class="search-card">
      <el-input
        v-model="keyword"
        placeholder="搜索公司名称或岗位..."
        clearable
        @keyup.enter="handleSearch"
      >
        <template #append>
          <el-button :icon="Search" @click="handleSearch">搜索</el-button>
        </template>
      </el-input>
    </el-card>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-wrapper">
      <el-icon class="is-loading"><Loading /></el-icon>
      <span>加载中...</span>
    </div>

    <!-- 无数据 -->
    <el-empty v-else-if="experiences.length === 0" description="暂无面试经验" />

    <!-- 经验列表 -->
    <div v-else class="experience-list">
      <el-card 
        v-for="exp in experiences" 
        :key="exp.experience_id" 
        shadow="hover"
        class="experience-card"
      >
        <div class="card-header">
          <div class="company-info">
            <h3>{{ exp.company_name }} - {{ exp.position }}</h3>
            <div class="meta">
              <el-tag size="small" type="info">
                {{ exp.is_anonymous ? '匿名用户' : `用户${exp.user_id}` }}
              </el-tag>
              <span class="date">{{ formatDate(exp.created_at) }}</span>
            </div>
          </div>
        </div>
        
        <div class="content">
          <p class="experience-text">{{ getExcerpt(exp.experience_content) }}</p>
        </div>
        
        <div class="footer">
          <div class="comment-count">
            <el-icon><ChatDotRound /></el-icon>
            <span>{{ exp.comment_count || 0 }} 条评论</span>
          </div>
          <div class="actions">
            <el-button type="primary" size="small" @click="goToDetail(exp.experience_id)">
              查看详情
            </el-button>
          </div>        </div>
      </el-card>
    </div>

    <!-- 分页 -->
    <el-pagination
      v-if="pagination.pages > 1"
      class="pagination"
      :current-page="pagination.current"
      :page-size="pagination.pageSize"
      :total="pagination.total"
      layout="prev, pager, next, total"
      @current-change="handlePageChange"
    />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Search, Loading, ChatDotRound, Plus } from '@element-plus/icons-vue'
import { interviewAPI } from '@/api/interview'

const router = useRouter()

const keyword = ref('')
const experiences = ref([])
const loading = ref(false)
const pagination = ref({
  current: 1,
  pageSize: 5,
  total: 0,
  pages: 0
})

// 加载经验列表
const loadExperiences = async (page = 1) => {
  loading.value = true
  try {
    const params = {
      page,
      pageSize: 5
    }
    if (keyword.value) {
      params.keyword = keyword.value
    }

    const response = await interviewAPI.getExperienceList(params)

    if (response.code === 200) {
      experiences.value = response.data || []
      if (response.pagination) {
        pagination.value = response.pagination
      }
    } else {
      experiences.value = []
      ElMessage.error(response.message || '获取经验列表失败')
    }
  } catch (e) {
    console.error('获取经验列表失败:', e)
    ElMessage.error('获取经验列表失败')
    experiences.value = []
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  pagination.value.current = 1
  loadExperiences(1)
}

// 分页处理
const handlePageChange = (page) => {
  pagination.value.current = page
  loadExperiences(page)
}

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN')
}

// 获取摘要
const getExcerpt = (content) => {
  if (!content) return ''
  return content.length > 200 ? content.substring(0, 200) + '...' : content
}

// 跳转到详情页
const goToDetail = (id) => {
  router.push(`/interview-experience/${id}`)
}

// 跳转到创建页面
const goToCreate = () => {
  router.push('/interview-experience/create')
}

onMounted(() => {
  loadExperiences()
})
</script>

<style scoped>
.interview-experience {
  width: 100%;
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  padding: 24px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(102, 126, 234, 0.15);
}

.header-left h1 {
  margin: 0 0 8px 0;
  color: #fff;
  font-size: 28px;
  font-weight: 600;
}

.description {
  margin: 0;
  color: rgba(255, 255, 255, 0.9);
  font-size: 14px;
}

.search-card {
  margin-bottom: 20px;
}

.loading-wrapper {
  text-align: center;
  padding: 60px 0;
  color: #666;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.is-loading {
  font-size: 24px;
}

.experience-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.experience-card {
  transition: all 0.3s;
}

.card-header {
  margin-bottom: 15px;
}

.company-info h3 {
  margin: 0 0 10px 0;
  color: #333;
  font-size: 20px;
  font-weight: 600;
}

.meta {
  display: flex;
  align-items: center;
  gap: 12px;
}

.date {
  font-size: 13px;
  color: #999;
}

.content {
  margin: 15px 0;
}

.experience-text {
  color: #666;
  line-height: 1.8;
  margin: 0;
  font-size: 14px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 15px;
  border-top: 1px solid #f0f0f0;
  margin-top: 15px;
}

.comment-count {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #666;
  font-size: 14px;
}

.actions {
  display: flex;
  gap: 10px;
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 30px;
}
</style>
